package system.student.funtion;

import system.student.entity.login.LoginRequest;
import system.student.entity.login.LoginResponse;
import system.student.entity.login.LoginType;
import system.student.entity.user.UserInfo;
import system.student.entity.user.UserType;
import system.student.funtion.context.LoginStrategyContext;
import system.student.funtion.context.SignupStrategyContext;
import system.student.funtion.exhibition.AdminExhibition;
import system.student.funtion.exhibition.StudentExhibition;
import system.student.funtion.exhibition.TeacherExhibition;
import system.student.util.EmailUtil;
import system.student.util.PasswordValidator;
import system.student.util.PhoneUtil;
import system.student.util.UsernameValidator;

import java.util.Scanner;

public class FunctionRunning {
    public static final Scanner RegisterScan = new Scanner(System.in);


    public static void runApplication(){
        boolean state = true;
        while(state){
            System.out.println("欢迎登录学生管理系统");
            System.out.println("注册请按1");
            System.out.println("登录请按2");
            System.out.println("退出请按3");
            Scanner scanner = new Scanner(System.in);
            String option = scanner.next();
            switch (option) {
                case "1": register();break;
                case "2": login();break;
                case "3":state = false;break;
                default:
                    System.out.println("其他功能正在开发中，敬请期待！");
                    break;
            }
        }
    }


    public static void register(){
        //是否已经完成注册
        String RegisterOption;
        boolean RegisterState = true;
        String username,password,phone,email;
        //基本信息写入
        username = IsValidUserName();
        password = IsValidPassword();
        phone = IsValidPhone();
        email = IsValidEmail();
        UserInfo userInfo = new UserInfo(username, password, phone,email);
        // 默认用户注册后状态为 1
        userInfo.setState(1);
        while(RegisterState){
            System.out.println("老师注册请按1");
            System.out.println("学生注册请按2");
            System.out.println("退出注册请按3");
            RegisterOption = RegisterScan.next();
            //用户类型选择
            switch (RegisterOption){
                case "1":
                    userInfo.setUserType(UserType.Teacher);
                    RegisterState = false;
                    break;
                case "2":
                    userInfo.setUserType(UserType.Student);
                    RegisterState = false;
                    break;
                case "3":
                    return;
                default:
                    System.out.println("输入有误，请重新选择身份,当前身份有1.学生,2.老师");
            }
        }
        SignupStrategyContext signupStrategyContext = new SignupStrategyContext();
        signupStrategyContext.signup(userInfo);
    }

    private static String IsValidUserName() {
        System.out.println("请输入您的用户名");
        String username = RegisterScan.next();
        while(true){
            if(!UsernameValidator.isValid(username)){
                System.out.println("请重新输入用户名");
                username = RegisterScan.next();
            }else
                break;
        }
        return username;
    }

    private static String IsValidEmail() {
        System.out.println("请输入您的邮箱");
        String email = RegisterScan.next();
        while(true){
            if(!EmailUtil.isValid(email)){
                System.out.println("邮箱格式错误,请重新输入邮箱");
                email = RegisterScan.next();
            }else
                break;
        }
        return email;
    }

    private static String IsValidPassword() {
        System.out.println("请输入您的密码(至少包含一个数字和字母,长度至少为2位)");
        String password = RegisterScan.next();
        while(true){
            if(!PasswordValidator.isValid(password)){
                System.out.println("密码格式错误,请重新输入密码");
                password = RegisterScan.next();
            }else
                break;
        }
        return password;
    }

    private static String IsValidPhone() {
        System.out.println("请输入手机号");
        String phone = RegisterScan.next();
        while(true){
            if(!PhoneUtil.isValid(phone)){
                System.out.println("手机号格式错误,请重新输入手机号");
                phone = RegisterScan.next();
            }else
                break;
        }
        return phone;
    }

    public static void login(){
        Scanner LoginScan = new Scanner(System.in);
        String LoginOption;
        boolean LoginState = true;
        //登录
        LoginRequest loginRequest = new LoginRequest();
        while(LoginState){
            System.out.println("账号密码登录请按1");
            System.out.println("手机验证码登录请按2");
            System.out.println("邮箱登录请按3");
            System.out.println("退出请按4");
            LoginOption = LoginScan.next();
            switch (LoginOption){
                case "1":
                    System.out.println("请输入用户名");
                    String username = LoginScan.next();
                    System.out.println("请输入密码");
                    String password = LoginScan.next();
                    loginRequest.setUsername(username);
                    loginRequest.setPassword(password);
                    loginRequest.setLoginType(LoginType.Password);
                    LoginState = false;
                    break;
                case "2":
                    String phone;
                    System.out.println("请输入手机号");
                    phone = LoginScan.next();
                    loginRequest.setPhone(phone);
                    loginRequest.setLoginType(LoginType.Phone);
                    LoginState = false;
                    break;
                case "3":
                    System.out.println("请输入邮箱");
                    String email = LoginScan.next();
                    loginRequest.setEmail(email);
                    loginRequest.setLoginType(LoginType.Email);
                    LoginState = false;
                    break;
                case "4":
                    return;
                default:
                    System.out.println("输入有误,请重新选择登陆方式");

            }
        }

        LoginStrategyContext loginStrategyContext = new LoginStrategyContext();
        LoginResponse login = loginStrategyContext.login(loginRequest);
        // 非空判断
        if (login == null){return;}
        if (login.getUserType() == UserType.Teacher) {
            TeacherExhibition.runningTeacherExhibition(login);
        }else if (login.getUserType() == UserType.Student) {
            StudentExhibition.runningStudentExhibition(login);
        } else if (login.getUserType() == UserType.Admin) {
            AdminExhibition.runningAdminExhibition(login);
        }
    }
}
